package com.leyou.user.web;

import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.utils.UserContext;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/info")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public ResponseEntity<UserDTO> queryInfo(
            @RequestParam("username")String username,@RequestParam("password")String password){
        return ResponseEntity.ok(this.userService.QueryInfo(username,password));
    }

    @PostMapping
    public ResponseEntity<Void> registerUser(@Valid User user,@RequestParam("code")String code){
        this.userService.registerUser(user,code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PostMapping("/code")
    public ResponseEntity<Void> sendSMS(@RequestParam("phone")String phone){
        this.userService.sendSMS(phone);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean> isExists(
            @PathVariable("data")String data,@PathVariable("type")Integer type){
        return ResponseEntity.ok(this.userService.isExists(data,type));
    }

    @GetMapping("/me")
    public ResponseEntity<UserDetail> getUserMessage(){
        return ResponseEntity.ok(UserContext.getUser());
    }
}
